package com.gzbugu.bugusys.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class isLogin implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpSession session = req.getSession();
		String contextPath = req.getContextPath();   //取得项目当前根目录
		String url=req.getRequestURI().substring(req.getRequestURI().lastIndexOf("/")+1, req.getRequestURI().length()); 
		String passport = (String) session.getAttribute("passport");// 获得session中的passport
		// passport 和 referenceId在session中存在
		String exclude= "index.html,.gif,.png"; 
	    if(exclude.indexOf(url)!=-1){
	    	chain.doFilter(request, response);
	    } else if (passport != null) {
			chain.doFilter(req, response);
		} else {
//			((HttpServletResponse)response).sendRedirect(contextPath+"/skip.jsp");
			((HttpServletResponse)response).sendRedirect(contextPath+"/index.html");
		}		
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
